//
//  HomeRecommandViewController.m
//  copyrightTrading
//
//  Created by edz on 2020/10/9.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "HomeRecommandViewController.h"
#import "VideoCell.h"
#import "HomeRecommendHeaderView.h"
#import "HomeRecmmendSectionOneCell.h"
#import "HomeRmdSectionTwoAndFourCell.h"
#import "HomeRmdSectionTwoCell.h"
#import "HomeRmdSectionThreeCell.h"
#import "UIViewController+VTMagic.h"
@interface HomeRecommandViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,HomeRecmmendSectionOneCellDelegate,HomeRecmmendSectionTwoCellDelegate>

@property (nonatomic, strong) UICollectionView *collectionView; // 模式列表
@property (nonatomic, assign) NSInteger pageIndex;

@end

@implementation HomeRecommandViewController

- (NSMutableArray *)vipList
{
    if (!_vipList) {
        _vipList = [NSMutableArray array];
    }
    return _vipList;
}

- (UIView *)listView {
    return self.view;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageIndex = 1;
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"yingxiangSelect" object:nil userInfo:@{@"selectID":@(0)}];
}

- (void)setupUI
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    layout.sectionInset = UIEdgeInsetsMake(20, 16, 20, 16);
//    layout.minimumLineSpacing = 14;
//    layout.minimumInteritemSpacing = 7;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = RGB_VALUE(0xFFFFFF);
//    self.stateCollectionView.backgroundColor = UIColor.clearColor;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.collectionView registerClass:[VideoCell class] forCellWithReuseIdentifier:@"VideoCell"];
    [self.collectionView registerClass:[HomeRecmmendSectionOneCell class] forCellWithReuseIdentifier:@"HomeRecmmendSectionOneCell"];
    [self.collectionView registerClass:[HomeRmdSectionTwoAndFourCell class] forCellWithReuseIdentifier:@"HomeRmdSectionTwoAndFourCell"];
    [self.collectionView registerClass:[HomeRmdSectionTwoCell class] forCellWithReuseIdentifier:@"HomeRmdSectionTwoCell"];
    [self.collectionView registerClass:[HomeRmdSectionThreeCell class] forCellWithReuseIdentifier:@"HomeRmdSectionThreeCell"];
    [self.collectionView registerClass:[HomeRecommendHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"HomeRecommendHeaderView"];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.offset(0);
    }];
    
    @weakify(self)
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.pageIndex = 0;
        [self requestData];
        [self requestAlldata];
    }];
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageIndex ++;
        [self requestData];
    }];
//    self.collectionView.mj_header.isCollectionViewAnimationBug = YES;
    [self requestData];
    [self requestAlldata];
}

- (void)reloadCollectionView
{
    [UIView performWithoutAnimation:^{
        [self.collectionView reloadData];
    }];
}
// 请求的推荐页面的其他分区数据
- (void)requestAlldata
{
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypeGet URLString:@"index" parameters:@{} done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        [self.collectionView.mj_header endRefreshing];
        self.commendList = responseObject[@"data"][@"commend_list"];
        self.yingxiangList = responseObject[@"data"][@"yingxiang_list"];
        self.adDic = responseObject[@"data"][@"adv"];
        self.fashionListArray = responseObject[@"data"][@"new_list"];
        [self reloadCollectionView];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [self.collectionView.mj_header endRefreshing];
    }];
}

// 请求的推荐页面的VIP下载专区
- (void)requestData
{
    NSDictionary *paramDic = @{
        @"type":@(1),
        @"sort_id":@"",
        @"keyword":@"",
        @"page":@(self.pageIndex),
        @"limit":@(10),
        @"is_commend":@(1)
    };
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypeGet URLString:@"material" parameters:paramDic done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if (self.pageIndex == [responseObject[@"data"][@"list"][@"page_sum"] integerValue]) {
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.collectionView.mj_footer endRefreshing];
        }
        
        if (self.pageIndex == 0) {
            [self.vipList removeAllObjects];
            [self.vipList addObjectsFromArray:responseObject[@"data"][@"list"][@"list"]];
        } else {
            [self.vipList addObjectsFromArray:responseObject[@"data"][@"list"][@"list"]];
        }
        [self reloadCollectionView];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [self.collectionView.mj_footer endRefreshing];
    }];
}

//- (void)setCommendList:(NSArray *)commendList
//{
//    _commendList = commendList;
//    [self.collectionView reloadData];
//}
//
//- (void)setFashionListArray:(NSArray *)fashionListArray
//{
//    _fashionListArray = fashionListArray;
//    [self.collectionView reloadData];
//}
//
//- (void)setYingxiangList:(NSArray *)yingxiangList
//{
//    _yingxiangList = yingxiangList;
//    [self.collectionView reloadData];
//}
//
//- (void)setVipList:(NSArray *)vipList
//{
//    _vipList = vipList;
//    [self.collectionView reloadData];
//}
//
//- (void)setAdDic:(NSDictionary *)adDic
//{
//    _adDic = adDic;
//    [self.collectionView reloadData];
//}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 5;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 1;
    } else if (section == 2) {
        return self.yingxiangList.count;
    } else if (section == 3) {
        return 1;
    } else if (section == 4) {
        return self.vipList.count;
    }
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(kDeviceWidth, 210+4);
    } else if (indexPath.section == 1)  {
        return CGSizeMake(kDeviceWidth, 117);
    } else if (indexPath.section == 2)  {
        return CGSizeMake((kDeviceWidth - 34 - 7)/2, 135);
    } else if (indexPath.section == 3)  {
        return CGSizeMake(kDeviceWidth, 122);
    } else if (indexPath.section == 4)  {
        return CGSizeMake((kDeviceWidth - 34 - 7)/2, 135);
    }
    return CGSizeZero;
}

// 定义每个Section的四边间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 4, 0);
    } else if (section == 1) {
        return UIEdgeInsetsMake(0, 0, 4, 0);
    } else if (section == 2) {
        return UIEdgeInsetsMake(0, 17, 0, 17);
    } else if ( section == 4) {
        return UIEdgeInsetsMake(0, 17, 10, 17);
    }
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
 
// 两行cell之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 14;
}
 
// 两列cell之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 7;
}

//设置header的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 1 || section == 2 || section == 4) {
        return CGSizeMake(kDeviceWidth, 40);
    } else {
        return CGSizeMake(0, 0);
    }
}
//设置footer的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(0, 0);
}
//设置header和footer的view
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    HomeRecommendHeaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HomeRecommendHeaderView" forIndexPath:indexPath];
    if (indexPath.section == 1) {
        headView.titleLabel.text = @"最新发布";
        return headView;
    } else if (indexPath.section == 2) {
        headView.titleLabel.text = @"影像馆";
        return headView;
    } else if (indexPath.section == 4) {
        headView.titleLabel.text = @"VIP下载专区";
    }
    return headView;
}

//- (UICollectionViewScrollDirection)col

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        HomeRecmmendSectionOneCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeRecmmendSectionOneCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.dataArray = self.commendList;
        return cell;
    } else if (indexPath.section == 1) {
        HomeRmdSectionTwoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeRmdSectionTwoCell" forIndexPath:indexPath];
        cell.dataArray = self.fashionListArray;
        cell.delegate = self;
        return cell;
    } else if (indexPath.section == 2) {
        HomeRmdSectionTwoAndFourCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeRmdSectionTwoAndFourCell" forIndexPath:indexPath];
        cell.dataDic = self.yingxiangList[indexPath.row];
        return cell;
    } else if (indexPath.section == 3) {
        HomeRmdSectionThreeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeRmdSectionThreeCell" forIndexPath:indexPath];
        cell.dataDic = self.adDic;
        return cell;
    } else if (indexPath.section == 4) {
        HomeRmdSectionTwoAndFourCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeRmdSectionTwoAndFourCell" forIndexPath:indexPath];
        cell.dataDic = self.vipList[indexPath.row];
        return cell;
    }
    
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
//        [self pushVideoDetailVCWithID:self.yingxiangList[indexPath.row][@"id"]];
        BaseTabbarController *tabBarController = (BaseTabbarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
        [tabBarController setSelectedIndex:1];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"yingxiangSelect" object:nil userInfo:@{@"selectID":@(indexPath.row)}];
    } else if (indexPath.section == 4) {
        [self pushVideoDetailVCWithID:self.vipList[indexPath.row][@"id"]];
    }
}

#pragma mark == cell的点击代理方法
- (void)tapSectionOneCellWithData:(NSDictionary *)data
{
//    VideoDetailViewController *vc = [[VideoDetailViewController alloc] init];
//    [self pushVc:vc];
    [self pushVideoDetailVCWithID:data[@"id"]];
}

- (void)tapSectionTwoCellWithData:(NSDictionary *)data
{
    [self pushVideoDetailVCWithID:data[@"id"]];
}
@end
